﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Win.Controls;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.ManageUtility;
using PlanningGIS.Util.Serialization;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 安全设置控件
    /// </summary>
    public partial class CtlSecuritySet : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlSecuritySet()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        private BillCommands _Commands;

        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public BillCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    BillCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }

        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldCmd">Old property value</param>
        /// <param name="newCmd">New property value</param>
        protected virtual void OnCommandsChanged(BillCommands oldCmd, BillCommands newCmd)
        {
            btnSave.Command = newCmd != null ? newCmd.SecuritySetCommands.Save : null;
            btnClose.Command = newCmd != null ? newCmd.SecuritySetCommands.Cancel : null;
        }

        /// <summary>
        /// 
        /// </summary>
        private SecuritySet _TempSecuritySet = null;

        /// <summary>
        /// 加载控件
        /// </summary>
        public void LoadControl()
        {
           
            this.Cursor = Cursors.WaitCursor;
            try
            {
                _TempSecuritySet = ManageHelper.SecuritySet;
                intErrorNumber.Value = ManageHelper.SecuritySet.ErrorNumber;
                intPasswordValid.Value = ManageHelper.SecuritySet.PasswordValid;
                intIdentifyTime.Value = (int)ManageHelper.SecuritySet.IdentifyTime;
                intLockTime.Value = (int)ManageHelper.SecuritySet.LockTime;
                chkAllowFZD.Checked = ManageHelper.SecuritySet.AllowFZD;
                chkShowSecurity.Checked = ManageHelper.SecuritySet.ShowSecurity;
                chkSplitUser.Checked = ManageHelper.SecuritySet.SplitUser;
                chkStartRegion.Checked = ManageHelper.SecuritySet.StartRegion;
                
                chkSplitUser.Visible = ManageHelper.CurrentUser.IsAdministrator();
                chkAllowFZD.Visible = ManageHelper.CurrentUser.IsAdministrator();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SecuritySet s = new SecuritySet()
                {
                    ErrorNumber = intErrorNumber.Value,
                    PasswordValid = intPasswordValid.Value,
                    IdentifyTime = intIdentifyTime.Value,
                    LockTime = intLockTime.Value,
                    AllowFZD = chkAllowFZD.Checked,
                    ShowSecurity = chkShowSecurity.Checked,
                    SplitUser = chkSplitUser.Checked,
                    StartRegion = chkStartRegion.Checked
                };
                ManageHelper.SecuritySet = s;
                //if (_TempSecuritySet.IdentifyTime != ManageHelper.SecuritySet.IdentifyTime)
                //    Log.WriteLog(ManageHelper.CurrentUser.Name, Log.zjgisLogType.ModifyIdentifyTime, Log.zjgisLogStatus.Success, "");
                //if (_TempSecuritySet.PasswordValid != ManageHelper.SecuritySet.PasswordValid)
                //    Log.WriteLog(ManageHelper.CurrentUser.Name, Log.zjgisLogType.ModifyPswValid, Log.zjgisLogStatus.Success, "");
                MsgHelper.ShowInfomationMsgbox(this.ParentForm, "保存成功");
                _TempSecuritySet = ManageHelper.SecuritySet;
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(this.ParentForm, "保存失败:" + ex.Message);
            }

        }

    }
}
